﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a component of a complex Taylor series.
    /// </summary>
    [Serializable]
    public class TaylorComplexSeriesComponent : ITaylorComplexSeriesComponent
    {
        /// <summary>
        /// The function about point a or x0 of the series.
        /// </summary>
        private ComplexNumber a;

        /// <summary>
        /// The exponent of the series component.
        /// </summary>
        private double exponent;

        /// <summary>
        /// The coefficient of the series component.
        /// </summary>
        private ComplexNumber coefficient;

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeriesComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the series component.</param>
        public TaylorComplexSeriesComponent(ComplexNumber coefficient)
        {
            this.a = new ComplexNumber();
            this.exponent = 1;
            this.coefficient = coefficient.Copy();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeriesComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the series component.</param>
        /// <param name="exponent">The exponent of the series component.</param>
        public TaylorComplexSeriesComponent(ComplexNumber coefficient, double exponent)
        {
            this.a = new ComplexNumber();
            this.exponent = exponent;
            this.coefficient = coefficient.Copy();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeriesComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the series component.</param>
        /// <param name="exponent">The exponent of the series component.</param>
        /// <param name="a">The function about point a or x0 of the series.</param>
        public TaylorComplexSeriesComponent(ComplexNumber coefficient, double exponent, ComplexNumber a)
        {
            this.a = a.Copy();
            this.exponent = exponent;
            this.coefficient = coefficient.Copy();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TaylorComplexSeriesComponent"/> class.
        /// </summary>
        /// <param name="component">The component to clone.</param>
        public TaylorComplexSeriesComponent(TaylorComplexSeriesComponent component)
        {
            if (component == (TaylorComplexSeriesComponent) null)
            {
                throw new ArgumentNullException("component");
            }

            this.a = component.A;
            this.exponent = component.Exponent;
            this.coefficient = component.Coefficient;
        }

        /// <summary>
        /// Solves the component at the specified position x.
        /// </summary>
        /// <param name="x">The specified position x.</param>
        /// <returns>The value of the specified position x.</returns>
        public ComplexNumber SolveAt(ComplexNumber x)
        {
            ComplexNumber tempuri = x - this.a;

            tempuri.Pow(this.exponent);

            return this.coefficient * tempuri;
        }

        /// <summary>
        /// Gets or sets the function about point a or x0 of the series.
        /// </summary>
        /// <value>The function about point a or x0 of the series.</value>
        public ComplexNumber A
        {
            get { return a; }
            set { a = value; }
        }

        /// <summary>
        /// Gets or sets the exponent of the series component.
        /// </summary>
        /// <value>The exponent of the series component.</value>
        public double Exponent
        {
            get { return exponent; }
            set { exponent = value; }
        }

        /// <summary>
        /// Gets or sets the coefficient of the series component.
        /// </summary>
        /// <value>The coefficient of the series component.</value>
        public ComplexNumber Coefficient
        {
            get { return coefficient; }
            set { coefficient = value; }
        }


        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public TaylorComplexSeriesComponent Copy()
        {
            return new TaylorComplexSeriesComponent(this.coefficient, this.exponent, this.a);
        }

        /// <summary>
        /// Compares the current component with another one.
        /// </summary>
        /// <param name="compare">The component to compare.</param>
        /// <returns>True if the components are even otherwise, false.</returns>
        public bool CompareTo(TaylorComplexSeriesComponent compare)
        {
            return (this == compare);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first TaylorSeriesComponent.</param>
        /// <param name="b">The second TaylorSeriesComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(TaylorComplexSeriesComponent a, TaylorComplexSeriesComponent b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.A == b.A) && (a.Exponent == b.Exponent) && (a.Coefficient == b.Coefficient));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first TaylorSeriesComponent.</param>
        /// <param name="b">The second TaylorSeriesComponent.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(TaylorComplexSeriesComponent a, TaylorComplexSeriesComponent b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.coefficient.ToString() + "(x - " + this.a.ToString() + ")^"
                   + this.exponent.ToString();
        }
    }
}